/**
 * Copyright 2005 SIB Framework

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
 */



package org.sibframework.utils;

public class Naming {
	private String fieldName;
	private String className;
	private String displayname;
	private String sqlname;

	private Naming(){}
	
	public static Naming createByDisplayName(String displayname) {
		String[] pices = displayname.split(" ");
		StringBuffer result = new StringBuffer();
		if (pices.length < 1) {
			return null;
		}
		result.append(pices[0]);
		for (int i = 1; i < pices.length; i++) {
			if (pices[i].length() < 1) {
				continue;
			}
			result.append(pices[i].substring(0, 1).toUpperCase());
			if (pices[i].length() < 2) {
				continue;
			}
			result.append(pices[i].substring(1).toLowerCase());
		}

		Naming naming =  new Naming();
		naming.setFieldName(result.toString());
		naming.setDisplayname(displayname);
		//naming.set
		return naming;
	}
	
	public static Naming createByFieldName(String fieldname) {
		Naming naming =  new Naming();
		naming.setFieldName(fieldname);
		naming.setSqlname(SqlUtility.property2Column(fieldname));
		return naming;
	}
	
	public static Naming createByMethodName(String methodname, String method) {
		Naming naming =  new Naming();
		String property = null;
		property = methodname.substring(method.length());
		property = property.substring(0, 1).toLowerCase() + property.substring(1);
		naming.setFieldName(property);
		naming.setSqlname(SqlUtility.property2Column(property));
		return naming;
	}
	
	public static Naming createBySqlName(String sqlname) {
		Naming naming =  new Naming();
		naming.setSqlname(sqlname);
		naming.setFieldName(SqlUtility.column2Property(sqlname));
		return naming;
	}

	
	
	
	/**
	 * @return Returns the displayname.
	 */
	public final String getDisplayname() {
		if (displayname != null) {
			return displayname;
		}
		if (fieldName.length() < 1) {
			return "";
		}

		StringBuffer result = new StringBuffer();
		result.append(fieldName.substring(0,1).toUpperCase());
		for (int i = 1; i < fieldName.length(); i++) {
			if (fieldName.charAt(i) <= 'Z' && fieldName.charAt(i) >= 'A') {
				if (result.charAt(result.length() - 1) != ' ') {
					if (i > 0 && fieldName.charAt(i - 1) <= 'Z' && fieldName.charAt(i - 1) >= 'A') {
						
					}else {
						if (result.charAt(result.length() - 1) == '_') {
							result.deleteCharAt(result.length() - 1);							
						} 
						result.append(" ");
					}
				}
				result.append(Character.toUpperCase(fieldName.charAt(i)));
			} else {
				result.append(Character.toLowerCase(fieldName.charAt(i)));
			}
		}
		return result.toString();
	}

	/**
	 * @param displayname The displayname to set.
	 */
	private final void setDisplayname(String displayname) {
		this.displayname = displayname;
	}

	/**
	 * @return Returns the methodname.
	 */
	public final String getMethodname(String method) {
		return method + fieldName.substring(0,1).toUpperCase() + fieldName.substring(1);
	}


	public String getClassName() {
		if (this.className == null) {
			this.className = String.valueOf(this.fieldName.charAt(0)).toUpperCase();
			this.className += this.fieldName.substring(1);
		}
		return className;
	}


	/**
	 * @return Returns the naming.
	 */
	public final String getFieldName() {
		return fieldName;
	}

	/**
	 * @param naming The naming to set.
	 */
	private final void setFieldName(String naming) {
		this.fieldName = naming;
	}

	/**
	 * @return Returns the sqlname.
	 */
	public final String getSqlname() {
		return sqlname;
	}

	/**
	 * @param sqlname The sqlname to set.
	 */
	private final void setSqlname(String sqlname) {
		this.sqlname = sqlname;
	}
}
